@model OrchardCore.Queries.Sql.ViewModels.AdminQueryViewModel
@using OrchardCore.ContentManagement;
@inject IContentManager ContentManager

@{
    var matchAllQuery = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(@"SELECT * FROM ContentItemIndex"));
}

<script asp-name="codemirror" depends-on="admin" at="Foot"></script>
<script asp-name="codemirror-mode-javascript" at="Foot"></script>
<script asp-name="codemirror-mode-sql" at="Foot"></script>

<h1>@RenderTitleSegments(T["SQL Query"])</h1>

<div asp-validation-summary="All" class="text-danger"></div>

<form asp-action="Query" method="post">
    <div class="row">
        <div class="col-md-6">

            <div class="form-group">
                <label asp-for="DecodedQuery">@T["Template"]</label>
                <textarea asp-for="DecodedQuery" rows="12" class="form-control"></textarea>
                <span class="hint">@T["You can <a href=\"{0}\">click here</a> to query all content items.", Html.Raw(Url.Action("Query", "Admin", new { area = "OrchardCore.Queries", Query = matchAllQuery }))]</span>
                <span class="hint">@T["The SQL query uses standard SQL92 syntax and is converted based on the actual database. Tables will be prefixed automatically."]</span>
            </div>
        </div>
        <div class="col-md-6">

            <div class="form-group">
                <label asp-for="Parameters">@T["Parameters"]</label>
                <textarea asp-for="Parameters" rows="1" class="form-control"></textarea>
                <span class="hint">@T["An optional Json object containing the parameter values for this query."]</span>
            </div>

        </div>
    </div>

    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <button type="submit" class="btn btn-primary">@T["Query"]</button>
            </div>
        </div>
    </div>
</form>
@if (Model.Elapsed != TimeSpan.Zero)
{
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <label asp-for="RawSql">@T["Final SQL Query"]</label>
                <textarea asp-for="RawSql" rows="12" class="form-control"></textarea>
                <span class="hint">@T["The query as it will be executed by the SQL database."]</span>
            </div>

            <div class="form-group">
                <a href="@Url.Action("Create", "Admin", new { area = "OrchardCore.Queries", id = "Sql", query = Model.DecodedQuery })" class="btn btn-success">@T["Create"]</a>
            </div>
        </div>
    </div>
}

@if (Model.Documents.Any())
{
    <span class="text-muted">@T["Query executed in {0} ms", Model.Elapsed.TotalMilliseconds]</span>

    @foreach (var document in Model.Documents)
    {
        <div>@Json.Serialize(document)</div>
    }
}

@{
    string sqlmode = "text/x-sql";

    if (Model.FactoryName.Contains("Sqlite"))
    {
        sqlmode = "text/x-sqlite";
    }
    else if (Model.FactoryName.Contains("Mysql"))
    {
        sqlmode = "text/x-mysql";
    }
    else if (Model.FactoryName.Contains("Npgsql"))
    {
        sqlmode = "text/x-pgsql";
    }
    else if (Model.FactoryName.Contains("Sql"))
    {
        sqlmode = "text/x-mssql";
    }
}

<script at="Foot">
    $(function () {
        CodeMirror.fromTextArea(document.getElementById('@Html.IdFor(m => m.DecodedQuery)'), { mode: "text/x-pgsql", lineNumbers: true, viewportMargin: Infinity });
        CodeMirror.fromTextArea(document.getElementById('@Html.IdFor(m => m.Parameters)'), { mode: "javascript", json: true, lineNumbers: true, viewportMargin: Infinity });
        if (rawSql = document.getElementById('@Html.IdFor(m => m.RawSql)')) {
          CodeMirror.fromTextArea(rawSql, { mode: "@sqlmode", lineNumbers: true, readonly: true, viewportMargin: Infinity });
        }
    });
</script>
